package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.CacheNames;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.bo.AreaBo;
import com.ruoyi.common.core.domain.entity.Area;
import com.ruoyi.common.core.domain.vo.AreaVo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.service.IAreaService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.redis.CacheUtils;
import com.ruoyi.system.mapper.AreaMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 行政区划Service业务层处理
 *
 * @author strongme
 * @date 2023-01-25
 */
@RequiredArgsConstructor
@Service
public class AreaServiceImpl implements IAreaService {

    private final AreaMapper baseMapper;

    @Cacheable(cacheNames = CacheNames.SYS_AREA, key = "#parId")
    @Override
    public List<AreaVo> queryByParId(String parId) {
        AreaBo areaBo = new AreaBo();
        areaBo.setParId(StrUtil.isBlank(parId) ? "-1" : parId);
        return queryList(areaBo);
    }

    /**
     * 查询行政区划
     */
    @Override
    @Cacheable(cacheNames = CacheNames.SYS_AREA_ID, key = "#id")
    public AreaVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询行政区划列表
     */
    @Override
    public TableDataInfo<AreaVo> queryPageList(AreaBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Area> lqw = buildQueryWrapper(bo);
        Page<AreaVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询行政区划列表
     */
    @Override
    public List<AreaVo> queryList(AreaBo bo) {
        LambdaQueryWrapper<Area> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Area> buildQueryWrapper(AreaBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Area> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getParId()), Area::getParId, bo.getParId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Area::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增行政区划
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(AreaBo bo) {
        Area add = BeanUtil.toBean(bo, Area.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        CacheUtils.clear(CacheNames.SYS_AREA_ID);
        CacheUtils.clear(CacheNames.SYS_AREA);
        return flag;
    }

    /**
     * 修改行政区划
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(AreaBo bo) {
        Area update = BeanUtil.toBean(bo, Area.class);
        validEntityBeforeSave(update);
        CacheUtils.clear(CacheNames.SYS_AREA_ID);
        CacheUtils.clear(CacheNames.SYS_AREA);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Area entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除行政区划
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        CacheUtils.clear(CacheNames.SYS_AREA_ID);
        CacheUtils.clear(CacheNames.SYS_AREA);
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
